<?php
/*
 * Joomla! 1.5 component ArticleCashRewards
 *
 * @version $Id: process.php 2009-05-10 01:47:01 svn $
 * @author Maverick
 * @package controllers
 * @subpackage ArticleCashRewards
 * @license GNU/GPL
 * Website: http://www.boddunan.com
 *
 * Review and approve the articles and assign points and cash rewards.
 * Users can see their articles and the points/cash rewards assigned in the front-end module.
 */

defined('_JEXEC') or die();

class ArticleCashRewardsControllerProcess extends ArticleCashRewardsController
{

	function __construct() {
		parent::__construct();
		
        global $mainframe, $acr_config;
        $acr_config = $mainframe->getUserState( SESSION_ACR_CONFIG );

        if(!isset($acr_config)){
            $model = & $this->getModel('config');
            $configt = $model->getConfiguration();

            foreach($configt as $ct){
                $acr_config[$ct->config_name] = $ct->config_value;
            }
            $mainframe->setUserState( SESSION_ACR_CONFIG, $acr_config );
        }
		$this->registerTask( 'save', 'store' );
		$this->registerTask( 'cancel', 'cancel' );
	}

	function display() {
		JRequest::setVar( 'view', 'process' );

		parent::display();
	}

	function cancel() {
		$msg = JText::_( 'Operation Cancelled' );
		$redirect = JRequest::getVar( 'redirect', 1, '', 'INTEGER');
        $this->setRedirects($redirect, $msg);
	}
	
	function store() {
		
		// get the model
		$cid = JRequest::getVar( 'article_id', '0', 'post', 'INTEGER');
		$cstate = JRequest::getVar( 'cstate', 0, '', 'INTEGER');
		$redirect = JRequest::getVar( 'redirect', 1, '', 'INTEGER');
		$model = $this->getModel('Process');
		
		// bind the array to the model and save it.
		if ($model->store()){
			$message = JText::_('Review Saved. ');
		} else {
			$message = JText::_('Review Save Failed');
			$message .= ' ['.$model->getError().'] ';
		}
		
		if ($model->approve($cstate, $cid)){
			$message .= JText::_('Article '.(($cstate == 1) ? 'approved.' : 'disapproved.'));
		}else{
			$message .= JText::_('Article'.(($cstate == 1) ? 'approval' : 'disapproval').' failed');
			$message .= ' ['.$model->getError().']';
		}
        $this->setRedirects($redirect, $message);
	}

    function setRedirects($redirect, $message){
        switch($redirect){
			case 1:
				$ctrlr  = "approval";
                $task   = "pendingreview";
				break;
			case 2:
				$ctrlr  = "listing";
                $task   = "approved";
				break;
			case 3:
				$ctrlr  = "rejected";
                $task   = "rejected";
				break;
			default:
				$ctrlr  = "approval";
                $task   = "cpanel";
				break;
		}
		$this->setRedirect('index.php?option=com_articlecashrewards&controller='.$ctrlr.'&task='.$task, $message);
    }
}
?>
